package 链表;

import common.ListNode;

/**
 * @author tmh
 * @date 2024/8/21 19:55
 * @description
 */
public class T2两数相加 {
    /**
     * 这里的条件是 || 条件
     * @param l1
     * @param l2
     * @return
     */
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        //定义一个链表来保存最后的结果
        ListNode result = new ListNode(0);
        ListNode p = result;
        int count = 0;
        while (l1 != null || l2 != null || count != 0) {
            int sum = 0;
            if (l1 != null) {
                sum += l1.val;
                l1 = l1.next;
            }
            if (l2 != null) {
                sum += l2.val;
                l2 = l2.next;
            }
            sum += count;
            count = sum >= 10 ? 1 : 0;
            p.next = new ListNode(sum - count * 10);
            p=p.next;
        }
            return result.next;
    }
}
